<?php
if (isset($_SESSION['sukses-checkout'])) {
	echo "<strong>".$_SESSION['sukses-checkout']."</strong> pesanan anda sudah masuk, cek email dah bero";
	unset($_SESSION['sukses-checkout']);
} else if (isset($_SESSION['keranjang'])) {
	echo "<style type=\"text/css\">
		table {
			border-collapse: collapse;
		}
		table,th,td {
			padding: 5px;
			border: 1px solid black;
		}
		.table-judul {
			font-weight: bold;
			text-align: center;
		}
		textarea {
			font-family: \"Times New Roman\", Times, serif;
		}
	</style>";
	if (isset($_SESSION['status-checkout'])) {
		echo "<p>".$_SESSION['status-checkout']."</p>";
		unset($_SESSION['status-checkout']);
	}
	?>
	<form action="<?php echo site() ?>/checkout-action" method="post" onsubmit="return validateCheckout()">
		Nama :<br><input type="text" name="nama" /><br><br>
		Email harus masih aktif dan lo bisa buka nanti rekening transfer dsb dsb gw kirim ke email ini :<br><input type="text" name="email" /><br><br>
		No HP harus bisa dihubungi nanti kalo ada apa2 gw ngubungin lo pake no ini<br>isinya hanya angka tidak ada spasi maupun karakter lain seperti "+","-" dll. contoh yang benar adalah 0812378123 :<br><input type="text" name="nohp" /><br><br>
		Alamat ini barang2 dikirimnya kemana :<br><textarea name="alamat"></textarea><br><br>
		<input type="submit" name="checkout" id="checkout" value="CheckOut" disabled="disabled" />
	</form>
	<script type="text/javascript">
		document.getElementById('checkout').removeAttribute("disabled");
		function validateCheckout() {
			var bulat = /^\d+$/;
			var body = document.getElementsByTagName("body")[0];
			var form = document.getElementsByTagName("form")[0];
			var nama = form["nama"].value;
			var email = form["email"].value;
			var nohp = form["nohp"].value;
			var alamat = form["alamat"].value;
			var status = document.getElementsByTagName("p")[0] || document.createElement("p");
			if (nama.length == 0 || email.length == 0 || nohp.length == 0 || alamat.length == 0) {
				status.innerHTML = "Semua input harus diisi";
				body.insertBefore(status,form);
			} else if (!bulat.test(nohp)) {
				status.innerHTML = "No HP hanya boleh angka";
				body.insertBefore(status,form);
			} else {
				if (document.getElementsByTagName('p')[0])
					body.removeChild(document.getElementsByTagName('p')[0]);
				return true;
			}
			return false;
		}
	</script>
	<?php
} else {
	if (isset($_SESSION['status-checkout'])) unset($_SESSION['status-checkout']);
	header("Location: ".site()."/keranjang");
}
?>